<template>
    <a-modal
            :title="title"
            :width="1000"
            :visible="visible"
            :confirmLoading="confirmLoading"
            :maskClosable="false"
            @ok="handleOk"
            @cancel="handleCancel"
            cancelText="关闭">

        <a-spin :spinning="confirmLoading">
            <a-form :form="form">
                <a-row :gutter="24">
                                                <a-col :md="12" :sm="8">
                                <a-form-item
                                        :labelCol="labelCol"
                                        :wrapperCol="wrapperCol"
                                        label="模板名称">
                                            <a-input placeholder="请输入模板名称"
                                                     v-decorator="['name', validatorRules.name]" :style="input_width"/>
                                </a-form-item>
                            </a-col>
                            <a-col :md="12" :sm="8">
                                <a-form-item
                                        :labelCol="labelCol"
                                        :wrapperCol="wrapperCol"
                                        label="模板内容">
                                </a-form-item>
                            </a-col>
                            <a-col :md="12" :sm="8">
                                <a-form-item
                                        :labelCol="labelCol"
                                        :wrapperCol="wrapperCol"
                                        label="状态">
                                            <j-dict-select-tag placeholder="请输入状态"
                                                               v-decorator="[ 'status', {}]"
                                                               dict-code="common_status"
                                                               :trigger-change="true" :style="input_width"></j-dict-select-tag>
                                </a-form-item>
                            </a-col>
                </a-row>

            </a-form>
        </a-spin>
    </a-modal>
</template>

<script>
    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import validator from "@/utils/validate";

    export default {
        name: "MoudualTemplateConfigModal",
        data() {
            return {
                title: "操作",
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                input_width:{width:'270px'},
                confirmLoading: false,
                form: this.$form.createForm(this),
                validatorRules: {
            name:
            {
                rules: [
                    {required: true, message: '请输入模板名称!'},
                    {whitespace: true},
                    {max:64, message: '长度不能大于64', trigger: 'blur'},

                ]
            }
        ,
            templateContent:
            {
                rules: [
                    {required: true, message: '请输入模板内容!'},

                ]
            }
        ,
            status:
            {
                rules: [

                ]
            }
        ,
        },
            url: {
                add: "/genertator/MoudualTemplateConfig/add",
                edit: "/genertator/MoudualTemplateConfig/edit",
            }
        ,
        }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'name', 'templateContent', 'status'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log(formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }
                })
            },
            handleCancel() {
                this.close()
            },
        }
    }
</script>

<style lang="less" scoped>

</style>
